package ctrl

/*******************************************************************************
Copyright:cloud
Author:cloudapex@126.com
Version:1.0
Date:2020-06-15
Description: 自定义日志接口[xorm]
*******************************************************************************/
import (
	"strings"

	"gitee.com/cloudapex/ulern/util"

	"xorm.io/xorm/log"
)

/*------------------------------------------------------------------------------
==================================XormLogger====================================
------------------------------------------------------------------------------*/
type XormLogger struct {
	Lv      log.LogLevel
	ShowSql bool
} //
func (this *XormLogger) Debug(v ...interface{}) {
	util.Cast(this.Lv <= log.LOG_DEBUG, func() { util.Log.Debugv(append([]interface{}{"XORM: "}, v...)) }, nil)
}
func (this *XormLogger) Debugf(format string, v ...interface{}) {
	util.Cast(this.Lv <= log.LOG_DEBUG, func() { util.Log.Debug("XORM: "+format, v...) }, nil)
}
func (this *XormLogger) Info(v ...interface{}) {
	util.Cast(this.Lv <= log.LOG_INFO, func() { util.Log.Infov(append([]interface{}{"XORM: "}, v...)) }, nil)
}
func (this *XormLogger) Infof(format string, v ...interface{}) {
	if strings.Contains(format, "PING ") {
		util.Log.Debug("XORM: "+format, v...)
		return
	}
	util.Cast(this.Lv <= log.LOG_INFO, func() { util.Log.Info("XORM: "+format, v...) }, nil)
}
func (this *XormLogger) Warn(v ...interface{}) {
	util.Cast(this.Lv <= log.LOG_WARNING, func() { util.Log.Warnv(append([]interface{}{"XORM: "}, v...)) }, nil)
}
func (this *XormLogger) Warnf(format string, v ...interface{}) {
	util.Cast(this.Lv <= log.LOG_WARNING, func() { util.Log.Warn("XORM: "+format, v...) }, nil)
}
func (this *XormLogger) Error(v ...interface{}) {
	util.Cast(this.Lv <= log.LOG_ERR, func() { util.Log.Errorv(append([]interface{}{"XORM: "}, v...)) }, nil)
}
func (this *XormLogger) Errorf(format string, v ...interface{}) {
	util.Cast(this.Lv <= log.LOG_ERR, func() { util.Log.Error("XORM: "+format, v...) }, nil)
}
func (this *XormLogger) Level() log.LogLevel { return this.Lv }

func (this *XormLogger) SetLevel(l log.LogLevel) { this.Lv = l }

func (this *XormLogger) ShowSQL(show ...bool) {
	util.Cast(len(show) == 0, func() { this.ShowSql = true }, func() { this.ShowSql = show[0] })
}
func (this *XormLogger) IsShowSQL() bool { return this.ShowSql }

func ConvXLogLv(lv util.ELogLevel) log.LogLevel {
	switch lv {
	case util.ELL_Trace:
		fallthrough
	case util.ELL_Debug:
		return log.LOG_DEBUG
	case util.ELL_Infos:
		return log.LOG_INFO
	case util.ELL_Warns:
		return log.LOG_WARNING
	case util.ELL_Error:
		fallthrough
	case util.ELL_Fatal:
		return log.LOG_ERR
	}
	return log.LOG_WARNING
}
